package cn.admin.config.common.protocol;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class AttributeElement implements ProtocolElement {

    public String name;
    public Serializable value;
    public static final String ATTRIBUTE_GROUP = "!Group";
    public static final String ATTRIBUTE_PERSISTENCY = "!Persist";
    public static final String ATTRIBUTE_REGISTRANT_TRACK = "!RegistrantTrack";
    public static final String ATTRIBUTE_ZIP = "!ZIP";
    private static final long serialVersionUID = 1L;

    public AttributeElement(String attrName, Serializable attrValue) {
        this.name = attrName;
        this.value = attrValue;
    }

    public String getName() {
        return this.name;
    }

    public Object getValue() {
        return this.value;
    }

    private void readObject(ObjectInputStream in) throws IOException {
        try {
            this.name = (String) in.readObject();
        } catch (ClassNotFoundException e) {
            throw new ProtocolException("Attribute name is not a string.");
        }

        try {
            this.value = (Serializable) in.readObject();
        } catch (ClassNotFoundException e) {
            throw new ProtocolException("Attribute value is not serializable.");
        }
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeObject(this.name);
        out.writeObject(this.value);
    }

    @Override
    public ProtocolElementUid uid() {
        return ProtocolElementUid.ELEMENT_ATTRIBUTE;
    }
}
